package com.api.aiy_changeStatus.Impl.Util;

import aiyh.utils.Util;
import aiyh.utils.zwl.common.ToolUtil;
import com.alibaba.fastjson.JSON;
import com.weaverboot.frame.ioc.handler.replace.weaReplaceParam.impl.WeaAfterReplaceParam;
import com.weaverboot.frame.ioc.handler.replace.weaReplaceParam.impl.WeaBeforeReplaceParam;
import org.h2.util.StringUtils;
import weaver.conn.RecordSet;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author EBU7-dev1-ayh
 * @create 2021/11/27 0027 13:28
 * util
 */

// TODO 去除日志文件打印相关
public class ChangeStatusUtil {
	private final static ToolUtil TOOL_UTIL = new ToolUtil();

	private final static RecordSet rs = new RecordSet();


	public static void changeHrmResourceStatus(WeaBeforeReplaceParam weaBeforeReplaceParam, String key, int status) {
		String apiUrl = weaBeforeReplaceParam.getApiUrl();
		Map<String, Object> paramMap = weaBeforeReplaceParam.getParamMap();
		String id = Util.null2String(paramMap.get(key));
		if (!StringUtils.isNullOrEmpty(id)) {
			ChangeStatusUtil.changeStatus(id, status);
		}
		TOOL_UTIL.writeDebuggerLog(String.format("进入拦截请求方法，获取到的api{%s}，请求参数{%s}", apiUrl, JSON.toJSONString(paramMap)));
	}

	public static void changeHrmResourceStatusByIds(WeaBeforeReplaceParam weaBeforeReplaceParam, String key, int status) {
		String apiUrl = weaBeforeReplaceParam.getApiUrl();
		Map<String, Object> paramMap = weaBeforeReplaceParam.getParamMap();
		String id = Util.null2String(paramMap.get(key));
		if (!StringUtils.isNullOrEmpty(id)) {
			ChangeStatusUtil.changeStatusByIds(id, status);
		}
		TOOL_UTIL.writeDebuggerLog(String.format("进入拦截请求方法，获取到的api{%s}，请求参数{%s}", apiUrl, JSON.toJSONString(paramMap)));
	}

	public static void batchChangeHrmResourceStatus(WeaBeforeReplaceParam weaBeforeReplaceParam, String key, int status) {
		String apiUrl = weaBeforeReplaceParam.getApiUrl();
		Map<String, Object> paramMap = weaBeforeReplaceParam.getParamMap();
		List<Map<String, Object>> datas = (List<Map<String, Object>>) paramMap.get("datas");
		if (datas == null || datas.isEmpty()) {
			return;
		}
		List<String> ids = new ArrayList<>();
		for (Map<String, Object> data : datas) {
			String id = Util.null2String(data.get(key));
			if (!StringUtils.isNullOrEmpty(id)) {
				ids.add(id);
			}
		}
		if (!ids.isEmpty()) {
			ChangeStatusUtil.changeStatus(ids, status);
		}
		TOOL_UTIL.writeDebuggerLog(String.format("进入拦截请求方法，获取到的api{%s}，请求参数{%s}", apiUrl, JSON.toJSONString(paramMap)));
	}


	public static boolean changeStatus(List<String> ids, int status) {
		String sql = "update hrmresource set changestatus = ? where id in ( " + String.join(",", ids) + ")";
		TOOL_UTIL.writeDebuggerLog(sql + " ---------------> " + status);
		return rs.executeUpdate(sql, status);
	}

	public static boolean changeStatusByIds(String ids, int status) {
		String sql = "update hrmresource set changestatus = ? where id in ( " + ids + ")";
		TOOL_UTIL.writeDebuggerLog(sql + " ---------------> " + status);
		return rs.executeUpdate(sql, status);
	}


	public static boolean changeStatus(String id, int status) {
		String sql = "update hrmresource set changestatus = ? where id = ?";
		TOOL_UTIL.writeDebuggerLog(sql + " ---------------> " + id + "," + status);
		return rs.executeUpdate(sql, status, id);
	}


	public static void changeHrmResourceStatusAfter(WeaAfterReplaceParam weaAfterReplaceParam, String key, int status) {
		String apiUrl = weaAfterReplaceParam.getApiUrl();
		Map<String, Object> paramMap = weaAfterReplaceParam.getParamMap();
		String id = Util.null2String(paramMap.get(key));
		if (!StringUtils.isNullOrEmpty(id)) {
			ChangeStatusUtil.changeStatus(id, status);
		}
		TOOL_UTIL.writeDebuggerLog(String.format("进入拦截请求方法，获取到的api{%s}，请求参数{%s}", apiUrl, JSON.toJSONString(paramMap)));
	}

	public static void batchChangeHrmResourceStatusAfter(WeaAfterReplaceParam weaAfterReplaceParam, String key, int status) {
		String apiUrl = weaAfterReplaceParam.getApiUrl();
		Map<String, Object> paramMap = weaAfterReplaceParam.getParamMap();
		List<Map<String, Object>> datas = (List<Map<String, Object>>) paramMap.get("datas");
		if (datas == null || datas.isEmpty()) {
			return;
		}
		List<String> ids = new ArrayList<>();
		for (Map<String, Object> data : datas) {
			String id = Util.null2String(data.get(key));
			if (!StringUtils.isNullOrEmpty(id)) {
				ids.add(id);
			}
		}
		if (!ids.isEmpty()) {
			ChangeStatusUtil.changeStatus(ids, status);
		}
		TOOL_UTIL.writeDebuggerLog(String.format("进入拦截请求方法，获取到的api{%s}，请求参数{%s}", apiUrl, JSON.toJSONString(paramMap)));
	}

	public static void changeHrmResourceStatusByIdsAfter(WeaAfterReplaceParam weaAfterReplaceParam, String key, int status) {
		String apiUrl = weaAfterReplaceParam.getApiUrl();
		Map<String, Object> paramMap = weaAfterReplaceParam.getParamMap();
		String id = Util.null2String(paramMap.get(key));
		if (!StringUtils.isNullOrEmpty(id)) {
			ChangeStatusUtil.changeStatusByIds(id, status);
		}
		TOOL_UTIL.writeDebuggerLog(String.format("进入拦截请求方法，获取到的api{%s}，请求参数{%s}", apiUrl, JSON.toJSONString(paramMap)));
	}
}
